﻿using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Kindohm.KSynth.Library;

namespace Kindohm.KSynth.Feed
{
    public class GenericTextSynth : ITextSynth
    {
        int index;
        Dictionary<int, double> frequencyMap;
        Voice voice;
        uint sequenceCount;
        int speed = 1500;
        bool sliding;
        double nextFrequency;
        double slideAmount = 0;
        List<int> keywordIndeces;

        public GenericTextSynth()
        {
            this.keywordIndeces = new List<int>();
            this.voice = new Voice();

            Oscillator osc = new Oscillator();
            osc.WaveForm = new SawWaveForm();
            Oscillator osc2 = new Oscillator();
            osc2.WaveForm = new SawWaveForm();
            voice.Oscillators.Add(osc);
            voice.Oscillators.Add(osc2);
            osc.Pan = short.MinValue;
            osc2.Pan = short.MaxValue;
            osc2.PhaseShift = 50000;
            osc2.Detune = 1;
            this.SlideScale = .05;

            this.frequencyMap = new Dictionary<int, double>();
        }

        public int Speed
        {
            get { return this.speed; }
            set { this.speed = value; }
        }

        public double SlideScale { get; set; }

        public Voice Voice { get { return this.voice; } }

        public void Load(string data, List<int> keywordIndexData)
        {
            this.keywordIndeces = keywordIndexData;
            char[] chars = data.ToCharArray();
            this.index = 0;
            this.LoadFrequencies(chars);
            this.Reset();
        }

        public StereoSample GetSample()
        {
            if (!sliding & this.sequenceCount > this.speed)
            {
                this.sliding = true;
                index++;
                if (index >= this.frequencyMap.Count)
                    index = 0;

                this.nextFrequency = this.frequencyMap[index];

                if (this.nextFrequency < this.voice.Frequency)
                    this.slideAmount = -this.SlideScale;
                else
                    this.slideAmount = this.SlideScale;
                this.voice.Frequency = this.voice.Frequency + this.slideAmount;
                this.sequenceCount = 0;
            }
            else if (sliding)
            {
                this.voice.Frequency = this.voice.Frequency + this.slideAmount;
            }
            else
            {
                this.sequenceCount++;
            }

            if ((this.slideAmount > 0 & this.voice.Frequency >= this.nextFrequency)
                || (this.slideAmount < 0 & this.voice.Frequency <= this.nextFrequency))
            {
                this.sliding = false;
                this.voice.Frequency = this.nextFrequency;
            }

            StereoSample sample = this.voice.GetSample();
            return sample;
        }

        public int Index
        {
            get { return this.index; }
        }

        public int Length
        {
            get { return this.frequencyMap.Count - 1; }
        }

        public void Reset()
        {
            index = 0;
            this.sequenceCount = uint.MaxValue / 2;
            this.sliding = false;
            this.nextFrequency = 40;
        }

        void LoadFrequencies(char[] chars)
        {
            this.frequencyMap.Clear();
            byte[] values = System.Text.UTF8Encoding.UTF8.GetBytes(chars);

            for (int i = 0; i < values.Length; i++)
            {
                if (this.keywordIndeces.Contains(i))
                {
                    this.frequencyMap.Add(i, values[i] * 15);
                }
                else
                    this.frequencyMap.Add(i, values[i] * 3);
            }
        }

    }
}
